home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 422_02 / dosutil / timeit.c < prev    next >
C/C++ Source or Header  |  1994-03-20  |  2KB  |  78 lines

  1. /*
  2.  * Program to time the execution of a DOS command
  3.  *
  4.  * Copyright 1991-1994 Dave Dunfield
  5.  * All rights reserved.
  6.  *
  7.  * Permission granted for personal (non-commercial) use only.
  8.  *
  9.  * Compile command: cc timeit -fop
  10.  */
  11. #include <stdio.h>
  12.  
  13. char timestamp[4], char command[129];
  14.  
  15. main(argc, argv)
  16.     int argc;
  17.     char *argv[];
  18. {
  19.     int i;
  20.     char *ptr;
  21.  
  22.     if(argc < 2)
  23.         abort("\nUse: timeit <command>\n\nCopyright 1991-1994 Dave Dunfield\nAll rights reserved.\n");
  24.  
  25.     /* concatinate all arguments into a single string */
  26.     ptr = command;
  27.     for(i = 1; i < argc; ++i) {
  28.         ptr=strcpy(ptr, argv[i]);
  29.         *ptr++ = ' '; }
  30.     *--ptr = 0;
  31.  
  32.     startimer(timestamp);            /* Record initial time */
  33.     system(command);                /* Execute the command */
  34.     elapsed(timestamp);                /* Calculate elapsed time */
  35.  
  36.     printf("Elapsed time: %02d:%02d:%02d.%02d",
  37.         timestamp[1], timestamp[0], timestamp[3], timestamp[2]);
  38. }
  39.  
  40. /*
  41.  * Record system time for later calculation
  42.  */
  43. startimer() asm
  44. {
  45.         MOV        AH,2CH        ; Get time function
  46.         INT        21H            ; Ask DOS
  47.         MOV        SI,4[BP]    ; Get pointer to timestamp
  48.         MOV        [SI],CX        ; Record hours & minites
  49.         MOV        2[SI],DX    ; Record seconds & hundreds
  50. }
  51.  
  52. /*
  53.  * Calculate elapsed time since timestamp recorded
  54.  */
  55. elapsed() asm
  56. {
  57.         MOV        AH,2CH        ; Get time function
  58.         INT        21H            ; Ask DOS
  59.         MOV        SI,4[BP]    ; Pointer to timestamp
  60.         SUB        DL,2[SI]    ; Convert 100ths
  61.         JNC        ELAP1        ; No borrow
  62.         ADD        DL,100        ; Re-adjust
  63.         DEC        DH            ; Reduce seconds
  64. ELAP1:    MOV        2[SI],DL    ; Save elapsed hundreds
  65.         SUB        DH,3[SI]    ; Convert seconds
  66.         JNS        ELAP2        ; No borrow
  67.         ADD        DH,60        ; Re-adjust
  68.         DEC        CL            ; Reduce minites
  69. ELAP2:    MOV        3[SI],DH    ; Save elapsed seconds
  70.         SUB        CL,[SI]        ; Convert minites
  71.         JNS        ELAP3        ; No borrow
  72.         ADD        CL,60        ; Re-adjust
  73.         DEC        CH            ; Adjust hours
  74. ELAP3:    MOV        [SI],CL        ; Save elapsed minites
  75.         SUB        CH,1[SI]    ; Convert hours
  76.         MOV        1[SI],CH    ; Save elapsed hours
  77. }
  78.